package com.cy.wangly.rest.controller;

import com.cy.wangly.rest.vo.TestVO;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Random;


@RestController
@RequestMapping(value = "/object", method = RequestMethod.DELETE)
public class ObjectController {
    @RequestMapping(value = "/getNum", method = RequestMethod.GET)
    public Integer getRandomInt() {
        Random random = new Random();
        Integer num = random.nextInt(100);
        return num;
    }

    @RequestMapping(value = "/addOne", method = RequestMethod.GET)
    public Integer addOne(@RequestParam("num") Integer num) {
        return num + 1;
    }

    @RequestMapping(value = "/getOpposite")
    public Boolean getOpposite(Boolean b) {
        if (null == b) {
            return true;
        }
        return !b;
    }

    @RequestMapping(value = "/getObject")
    public TestVO getObject() {
        TestVO vo = new TestVO();
        vo.setName("wangly");
        vo.setListValue(ImmutableList.of("wangly", "yanhy", "zhour"));
        vo.setStringMap(ImmutableMap.of("wangly", "1", "yanhy", "2", "zhour", "3"));
        System.out.println(vo.toString());
        return vo;
    }

    @RequestMapping(value = "/setObject")
    public TestVO setObject(TestVO vo) {
        vo.setName(vo.getName() + ",ok");
        if (!CollectionUtils.isEmpty(vo.getListValue())) {
            for (String s : vo.getListValue()) {
                vo.getListValue().remove(s);
                vo.getListValue().add(s + ",ok");
            }
        }
        vo.setStringMap(ImmutableMap.of("wangly", "1", "yanhy", "2", "zhour", "3"));
        return vo;
    }

    @PostMapping(value = "/getEcho")
    public Map echo(@RequestBody String b) {
//        try {
//            Thread.sleep(40000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        System.out.println(b);
        return ImmutableMap.of("httpCode",200, "msg", b);
    }
}
